package com.sebiotec.ambassador.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.sebiotec.ambassador.model.post.PostIsRead;
import com.sebiotec.ambassador.model.post.PostSearchMessageBean;
import com.sebiotec.ambassador.model.response.MessListResponse;
import com.sebiotec.ambassador.net.BookApiHelper;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.language.LocalManageUtil;
import com.sebiotec.basecommon.utils.tools.ActivityTool;
import com.sebiotec.basecommon.utils.tools.SPUtils;


import java.util.HashMap;

import okhttp3.ResponseBody;

public class MessageViewModel extends AndroidViewModel {
    public MessageViewModel(@NonNull Application application) {
        super(application);
    }

    public MyHistoryDataCallBack mDataCallBack;

    //数据回调接口
    public interface MyHistoryDataCallBack<T> {
        void onDataSuccessCallBack(T t);
        void onDataFailCallBack(T t);
    }


    //获取UpComing数据
    public void   getData(String filte,int pagesize,int pageIndex, MyHistoryDataCallBack<Object> mDataCallBack){
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
        map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
        map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
        PostSearchMessageBean bean =new PostSearchMessageBean(filte,pageIndex,pagesize);
        BookApiHelper.geRadomApi()
                .MessageList(map,new BaseHttpUtil<PostSearchMessageBean>().convertVo2Json(bean))
                .compose(Transformer.<MessListResponse>switchSchedulers())
                .subscribe(new CommonObserver<MessListResponse>() {
                    @Override
                    protected void onError(String errorMsg) {
                        mDataCallBack.onDataFailCallBack(errorMsg);
                    }

                    @Override
                    protected void onSuccess(MessListResponse s) {

                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });

    }


   //通知已读
   public  void  isRead(String id,int Status,MyHistoryDataCallBack mDataCallBack){

       HashMap<String,Object> map=new HashMap<String, Object>();
       map.put("LoginId", SPUtils.get(SjmConstant.LOGINID,""));
       map.put("Token", SPUtils.get(SjmConstant.TOKEN,""));
       map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
       PostIsRead bean =new PostIsRead(id,Status);
       BookApiHelper.geRadomApi()
               .isRead(map,id,new BaseHttpUtil<PostIsRead>().convertVo2Json(bean))
               .compose(Transformer.<ResponseBody>switchSchedulers())
               .subscribe(new CommonObserver<ResponseBody>() {
                   @Override
                   protected void onError(String errorMsg) {
                       mDataCallBack.onDataFailCallBack(errorMsg);
                   }

                   @Override
                   protected void onSuccess(ResponseBody s) {

                       mDataCallBack.onDataSuccessCallBack(s);
                   }
               });

   }




}
